<template>
	<div class="fix_a white">
		<van-nav-bar 
		  class="fix_header"
		  title="新增收货地址"
		  left-arrow
		  @click-left="onClickLeft"
		>		
		  <van-icon name="arrow-left" slot="left" color="#000" size="20"/>
	    </van-nav-bar>	
	    <div class="fix_next"></div>
	    
	    <div class="input">
	    	<van-cell :title="title" is-link class="cell" @click="show=true"/>
			<van-cell-group>
			  <van-field v-model="params.address" placeholder="请输入详细地址" />
			</van-cell-group>
			<van-cell-group>
			  <van-field v-model="params.addressRealName" placeholder="请输入收货人姓名" />
			</van-cell-group>
			<van-cell-group>
			  <van-field v-model="params.addressMobile" placeholder="请输入收货人联系电话" />
			</van-cell-group>
			<van-cell-group>
			  <van-switch-cell v-model="params.addressIsDefault" title="是否设为默认地址" 
			  	active-color="#ff5603" 
			  	inactive-color="#eeeeee"/>
			</van-cell-group>			
	    </div>
        <van-popup
           v-model="show"
           position="bottom"
           :style="{ height: '50%' }"
        >
        <van-area :area-list="areaList" @cancel="show=false" @confirm="confirm"/>        
        
        </van-popup>
	    <div class="fix_bottom" @click="save">
	    	<van-button type="primary"  class="bottom" :loading="loading">保存地址</van-button>
	    </div>
	</div>	
</template>

<script>
import areaList from '@/api/area.js'
import {getFun} from '@/api/publicFun.js'	 
let publicFun=getFun()	
export default {	
	data() {
       return {
       	 comId:'',
       	 loading:false,
       	 title:'请选择省市区', // 选择省市区的默认文字
       	 show:false,// 显示地区选择
       	 areaList:areaList,   // 地区选择的数据文件
         url1:'/shop/address/add',  // 新增地址接口
         url2:'/shop/address/toedit',//获取详情接口
         params:{
			address:'',           // 详细地址 ,
			addressAreaId1:'',    // 一级地区Id ,
			addressAreaId2:'',    // 二级地区Id ,
			addressAreaId3:'',    // 三级地区Id ,
			addressIsDefault:false,  // 是否是默认收货地址(0-否，1-是) ,
			addressMobile:'',     // 手机 ,
			addressRealName:'',   // 收货人姓名 ,
			memberId:'',          // 会员Id     
			addressAreaInfo:''    // 拼接地区 
         }
       }
	},
	methods: {
		// 确认省市区
        confirm(arr){
        	this.params.addressAreaId1=arr[0].code
        	this.params.addressAreaId2=arr[1].code
        	this.params.addressAreaId3=arr[2].code
        	this.title=arr[0].name+' '+ arr[1].name +' ' + arr[2].name 
        	this.params.addressAreaInfo=this.title
        	this.show=false
        },
        save(){
        	if(!this.regTel(this.params.addressMobile)){
        		this.$toast('请输入正确的电话号码')
        		return false
        	}        	
        	if(this.params.addressAreaId1==''||this.params.addressAreaId2==''||this.params.addressAreaId3==''){
        		this.$toast('请选择省市区')
        		return false
        	}
        	if(!this.params.address){
        		this.$toast('详细地址不能为空')
        		return false        		
        	}
        	if(!this.params.addressRealName){
        		this.$toast('收货人姓名不能为空')
        		return false        		
        	}        	
            this.params.addressIsDefault=this.params.addressIsDefault?1:0  
            this.loading=true
	      	let that=this	      	
		    this.$Axios.Post(this.url1,this.params)
		    .then(function(res){	
			   let arg='[{"comId":"'+that.comId+'"},{"userId":"'+that.params.memberId+'"}]' 
			   that.link('/market/order/buy/address',arg)			
               that.loading=false
		    })	            
        }
	},
	mixins:[publicFun],//混入		
	mounted() {
		this.params.memberId=this.$router.currentRoute.query.userId
		this.comId=this.$router.currentRoute.query.comId
	}
}	
</script>

<style scoped lang="scss">
.bottom{background: $common-bg1;border: none;width: 100%;border-radius: 0;}

</style>